package com.gushenge.core.requests

import android.app.Activity
import android.content.Context
import android.util.Log
import com.gushenge.core.*
import com.gushenge.core.base.Base.BASEURL
import com.gushenge.core.base.Base.BASEURLV3
import com.gushenge.core.beans.*
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.uid
import com.rxlife.coroutine.RxLifeScope
import org.jetbrains.anko.runOnUiThread
import rxhttp.onErrorReturn
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

object UserRequest {
    // 分享好友
    fun shareFriend(listener:ShareNewBean. () -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.ShareFriend)
                .add("uid",uid)
                .add("t", time)
                .add("sign", httpSign(uid))
                .add("member_id",MMKVConsts.sub)
                .add("language", MMKVConsts.language)
                .toClass<Code<ShareNewBean>>()
                .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.toast()
            }
        }
    }
    fun getSmallAccountList(listener:ArrayList<SmallAccountGame>.()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.SMALL_ACCOUNT_MANAGER)
                    .add("uid",uid)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("Version","3.0")
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<SmallAccountGame>>()
                    .await()
            if (result.code==1){
                result.data.listener()
            }else{
                result.message.toast()
            }
        }
    }
    fun setMainSmallAccount(id:String,listener:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.SMALL_ACCOUNT_SELECT_MAIN)
                    .add("uid",uid)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("id", id)
                    .add("member_id",MMKVConsts.sub)
                    .add("Version","3.0")
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<SmallAccountGame>>()
                    .await()
            if (result.code==1){
                listener()
            }else{
                result.message.toast()
            }
        }
    }


    fun userCenter(listener: UserCenter?.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.USER_CENTER_INFO)
                    .add("uid",uid)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<UserCenter>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }else if(result.code == 100){
                listener(null)
            } else{
                result.message.toast()
            }
        }
    }

    fun newUserCenter(listener: UserCenter?.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.New_USER_CENTER_INFO)
                .add("uid",uid)
                .add("t", time)
                .add("sign", httpSign(uid))
                .add("member_id",MMKVConsts.sub)
                .add("language", MMKVConsts.language)
                .toClass<Code<UserCenter>>()
                .await()
            if (result.code==1){
                result.data?.listener()
            }else if(result.code == 100){
                listener(null)
            } else{
                result.message.toast()
            }
        }
    }

    fun smallDel(smallId: String, listener: ()->Unit) {
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.SMALL_DEL)
                    .add("uid",uid)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("member_id",MMKVConsts.sub)
                    .add("small_uid", smallId)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            if (result.code==1){
                listener()
            }else{
                result.message.toast()
            }
        }
    }

    fun wealFunction(listener: WealCodes<Nav>.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.WEAL_FUNCTION)
                    .add("uid",uid)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<WealCodes<Nav>>()
                    .await()
            if (result.code==1){
                result.listener()
            }else{
                result.message.toast()
            }
        }
    }
    fun wealHuodong(listener: ArrayList<WealDoing>.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.WEAL_HUODONG)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<WealDoing>>()
                    .await()
            if (result.code==1){
                result.data.listener()
            }else{
                result.message.toast()
            }
        }
    }

    fun getCodeByPhoneLogin(phone: String,phoneCode: String,listener: SmsResult.() -> Unit){
//        when{
//            phone.length < 11 -> "请输入正确的手机号".toast()
//            else ->{
                RxLifeScope().launch {
                    val result = RxHttp.get(GlobalConsts.GETCODEBYPHONELOGIN)
                            .add("phone", phone)
                            .add("phoneCode",phoneCode)
                            .add("member_id",MMKVConsts.sub)
                            .add("language", MMKVConsts.language)
                            .toClass<Code<SmsResult>>()
                            .await()
                    result.message.toast()
                    if (result.code==1){
                        result.data?.listener()
                    }
                }
//            }
//        }
    }

    fun register(phone: String,phoneCode: String,isRegister:Boolean, password: String, sessionid: String, code: String,share:String, listener: ()->Unit) {
        when{
            phone.length<11 -> "请输入正确的手机号".toast()
            (isRegister && password.length<6) -> "密码不能小于六位".toast()
            sessionid.isBlank() -> "请获取验证码".toast()
            code.isBlank() -> "请输入验证码".toast()
            else -> {
                RxLifeScope().launch {
                    val result = RxHttp.get(GlobalConsts.REGBYPHONE)
                            .add("phone", phone)
                            .add("phoneCode", phoneCode)
                            .add("passwd", password)
                            .add("sessionid", sessionid)
                            .add("code", code)
                            .add("invite", share)
                            .add("member_id", MMKVConsts.sub)
                            .add("language", MMKVConsts.language)
                            .toClass<Code<String>>()
                            .await()
                    if (result.code==1){
                        result.data?.let{

                            MMKVConsts.uid = it
                        }
                        listener()
                    }else{
                        result.message.toast()
                    }
                }
            }
        }
    }

    fun registerByUserName(userName:String,password: String,password2: String,code: String,listener: () -> Unit){
        when{
            userName.isBlank() -> "用户名不能为空".toast()
            userName.length < 6 -> "用户名不能小于6位".toast()
            password.isBlank() -> "密码不能为空".toast()
            password.length < 6 -> "密码不能小于6位".toast()
            password != password2 -> "两次输入的密码不一致".toast()
            else->{
                RxLifeScope().launch {
                    val result = RxHttp.get(GlobalConsts.REGBYUSERNAME)
                            .add("user_name", userName)
                            .add("passwd", password)
                            .add("t", time)
                            .add("sign",httpSign(userName,password))
                            .add("invite", code)
                            .add("member_id", MMKVConsts.sub)
                            .add("language", MMKVConsts.language)
                            .toClass<Code<String>>()
                            .await()
                    if (result.code==1){
                        result.data?.let{
                            MMKVConsts.uid = it
                        }
                        listener()
                    }else{
                        result.message.toast()
                    }
                }
            }
        }
    }


    fun myAssets(listener: MyAssets.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.MYASSETS)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign",httpSign(uid))
                    .add("Version", "3.0")
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<MyAssets>>()
                    .await()
            if (result.code==1){
                result.data?.listener()
            }else{
                result.message.toast()
            }
        }
    }

    /*我的评论*/
    fun myDiscuss(p:Int,listener:(p:Int,max:Int,data:ArrayList<Discuss>)->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.MYDISCUSS)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("p",p)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<Discuss>>()
                    .await()
            if(result.code==1){
                listener(result.p,result.max_p,result.data)
            }else{
                result.message.toast()
            }
        }
    }

    /*礼包中心*/
    fun giftCenter(key:String,type:Int,p:Int,listener:(p:Int,max:Int,data:ArrayList<Game>)->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.GIFTCENTER)
                    .add("type", type)
                    .add("keyword", key)
                .add("member_id",MMKVConsts.sub)
                    .add("p", p)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<Game>>()
                    .await()
            if(result.code==1){
                listener(result.p,result.max_p,result.data)
            }else{
                result.message.toast()
            }
        }
    }

    /*礼包中心*/
    fun giftCenterNew(key:String,type:Int,p:Int,listener:(p:Int,max:Int,data:ArrayList<GameGift>)->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.GIFTCENTER)
                .add("type", type)
                .add("keyword", key)
                .add("member_id",MMKVConsts.sub)
                .add("p", p)
                .add("language", MMKVConsts.language)
                .toClass<Codes<GameGift>>()
                .await()
            if(result.code==1){
                listener(result.p,result.max_p,result.data)
            }else{
                result.message.toast()
            }
        }
    }


    /*删除评论*/
    fun myDiscussDelete(id: String,listener:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.MYDISCUSSDELETE)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("id",id)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            if(result.code==1){
                listener()
            }else{
                result.message.toast()
            }
        }
    }
    /*我的礼包*/
    fun myGift(type:Int,p:Int,listener:(p:Int,max:Int,data:ArrayList<Gift>)->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.MYGIFT)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("p",p)
                    .add("type",type)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<Gift>>()
                    .await()
            if(result.code==1){
                listener(result.p,result.max_p,result.data)
            }else{
                result.message.toast()
            }
        }
    }

    /*删除评论*/
    fun myGiftDelete(id: String,listener:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.MYGIFTDELETE)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("id",id)
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            if(result.code==1){
                listener()
            }else{
                result.message.toast()
            }
        }
    }
    /*是否新消息*/
    fun hasNewMessage(listener:Boolean.()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.ISHASNEWMESSAGE)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("Version","3.0")
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            Log.e( "hasNewMessage: ",result.toString() )
            (result.code==1).listener()
        }
    }

    /*消息中心*/
    fun notice(p:Int,listener:(p:Int,max:Int,data:ArrayList<Notice>)->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.NOTICE)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("p",p)
                    .add("Version","3.0")
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<Notice>>()
                    .await()
            if(result.code==1){
                listener(result.p,result.max_p,result.data)
            }else{
                result.message.toast()
            }
        }
    }

    /*消息已读*/
    fun noticeRead(id: Int,listener:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.NOTICEREAD)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("id", id)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("Version","3.0")
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            if(result.code==1){
                listener()
            }else{
                result.message.toast()
            }
        }
    }
    /*消息删除*/
    fun noticeDelete(id: Int,listener:()->Unit){
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.NOTICEDELETE)
                    .add("uid", MMKVConsts.uid)
                    .add("t", time)
                    .add("id", id)
                    .add("sign", httpSign(MMKVConsts.uid))
                    .add("Version","3.0")
                    .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            Log.e( "noticeDelete: ",result.toString() )
            if(result.code==1){
                listener()
            }else{
                result.message.toast()
            }
        }
    }

    fun task(context: Context, listener: TaskTop.()->Unit) {

        RxLifeScope().launch {
            val result =  RxHttp.get("$BASEURL?ct=newapp&ac=goldtask")
                    .add("uid",uid)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Code<TaskTop>>()
                    .await()
            result.apply {
                context.runOnUiThread {
                    if (code==1){
                        data?.listener()
                    }else{
                        message.toast()
                    }
                }
            }
        }
    }

    fun goldDetail(context: Activity, p:Int, listener: (ArrayList<GoldDetail>, Int, Int) -> Unit){
        RxLifeScope().launch {
            val result =  RxHttp.get("$BASEURL?ct=napp&ac=goldlist")
                    .add("uid",uid)
                    .add("p", p)
                .add("member_id",MMKVConsts.sub)
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<GoldDetail>>()
                    .await()
            result.apply {
                context.runOnUiThread {
                    if (code==1){
                        listener(data,this.p, this.max_p)
                    }else{
                        message.toast()
                    }
                }
            }
        }
    }
    fun Zhongjiang(context: Activity,listener: (ArrayList<GoldDetail>) -> Unit){
        RxLifeScope().launch {
            val result =  RxHttp.get("$BASEURL?ct=app&ac=choujiang_log")
                    .add("uid",uid)
                    .add("t", time)
                .add("member_id",MMKVConsts.sub)
                    .add("sign", httpSign(uid))
                    .add("language", MMKVConsts.language)
                    .toClass<Codes<GoldDetail>>()
                    .await()
            result.apply {
                context.runOnUiThread {
                    if (code==1){
                        listener(data)
                    }else{
                        message.toast()
                    }
                }
            }
        }
    }
    /*修改地址*/
    fun changeAddress(value: String, id: String, listener: ()->Unit) {
        RxLifeScope().launch {
            val result =  RxHttp.get(GlobalConsts.CHANGE_ADDRESS)
                    .add("id",id)
                    .add("value",value)
                    .add("uid",uid)
                .add("member_id",MMKVConsts.sub)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            result.apply {
                if (code==1){
                    listener()
                }else{
                    message.toast()
                }

            }
        }
    }

    /*修改地址*/
    fun safety( listener: SafetyV3.()->Unit) {
        RxLifeScope().launch {
            val result =  RxHttp.get("${BASEURLV3}/?ct=app&ac=anquan")
                    .add("uid",uid)
                    .add("t", time)
                .add("member_id",MMKVConsts.sub)
                    .add("sign", httpSign(uid))
                    .add("language", MMKVConsts.language)
                    .toClass<Code<SafetyV3>>()
                    .await()
            result.apply {
                if (code==1){
                    result.data?.listener()
                }else{
                    message.toast()
                }

            }
        }
    }

    fun getPhoneCodeByUid( listener: String.()->Unit) {
        RxLifeScope().launch {
            val result =  RxHttp.get("${BASEURLV3}/?ct=app&ac=user_codes")
                    .add("uid",uid)
                .add("member_id",MMKVConsts.sub)
                    .add("t", time)
                    .add("sign", httpSign(uid))
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            result.apply {
                if (code==1){
                    result.data?.listener()
                }else{
                    message.toast()
                }

            }
        }
    }

    fun getEmailCodeByUid( listener: String.()->Unit) {
        RxLifeScope().launch {
            val result =  RxHttp.get("${BASEURLV3}/?ct=app&ac=user_email_codes")
                    .add("uid",uid)
                    .add("t", time)
                .add("member_id",MMKVConsts.sub)
                    .add("sign", httpSign(uid))
                    .add("language", MMKVConsts.language)
                    .toClass<Code<String>>()
                    .await()
            result.apply {
                if (code==1){
                    result.data?.listener()
                }else{
                    message.toast()
                }

            }
        }
    }

    fun getUserCenter(listener: UserCenterBean?.() -> Unit){
        RxLifeScope().launch {
            val result =  RxHttp.get("${BASEURLV3}/?ct=app2022&ac=account")
                .add("uid",uid)
                .add("t", time)
                .add("member_id",MMKVConsts.sub)
                .add("sign", httpSign(uid))
                .add("imei", MMKVConsts.oaid)
                .add("language", MMKVConsts.language)
                .toClass<Code<UserCenterBean>>()
                .await()
            result.apply {
                when (code) {
                    1 -> {
                        result.data?.listener()
                    }
                    100 -> {
                        listener(null)
                    }
                    else -> {
                        message.toast()
                    }
                }

            }
        }
    }

    fun getHuiYuan(listener: HuiYuanBean.() -> Unit){
        RxLifeScope().launch {
            val result =  RxHttp.get("${BASEURLV3}/?ct=app2022&ac=huiyuan")
                .add("uid",uid)
                .add("t", time)
                .add("member_id",MMKVConsts.sub)
                .add("sign", httpSign(uid))
                .add("language", MMKVConsts.language)
                .toClass<Code<HuiYuanBean>>()
                .await()
            result.apply {
                if (code==1){
                    result.data?.listener()
                }else{
                    message.toast()
                }

            }
        }
    }

    fun openHuiYuan(id:String,payment : String,listener: String.() -> Unit){
        RxLifeScope().launch {
            val result =  RxHttp.get("${BASEURLV3}/?ct=app2022&ac=huiyuan_pay")
                .add("uid",uid)
                .add("t", time)
                .add("id", id)
                .add("member_id",MMKVConsts.sub)
                .add("payment",payment)
                .add("sign", httpSign(uid))
                .add("language", MMKVConsts.language)
                .toClass<Code<String>>()
                .await()
            result.apply {
                if (code==1){
                    data?.listener()
                }else{
                    message.toast()
                }

            }
        }
    }


   suspend fun scan(token:String?,listener:   () -> Unit){
        val result =  RxHttp.get("${BASEURLV3}/?ct=app2022&ac=saoyisao")
            .add("uid",uid)
            .add("t", time)
            .add("token", token)
            .add("member_id",MMKVConsts.sub)
            .add("sign", httpSign(uid))
            .add("language", MMKVConsts.language)
            .toClass<Code<String>>()
            .await()
        result.apply {
            if (code==1){
                 listener()
            }else{
                message.toast()
            }

        }
    }
}